package com.qfedu.rocketmq01.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/13 17:13
 * @Modified:
 */
@RestController
@RequestMapping("api/mqorder/")
@Tag(name = "顺序消息")
@Slf4j
public class MqOrderSend {

    @Resource
    private RocketMQTemplate template;

    @GetMapping("send1")
    public String send1(String msg,String group){
        //1.创建消息
//        Message message= MessageBuilder.createMessage(msg,null);
        //2.发送顺序消息 同步
        //syncSendOrderly 参数说明：1.主题 2.消息 Message 对象，字符串 3.消息组名
        SendResult result=template.syncSendOrderly("xingmq01",msg,group);
        return result.getSendStatus().name();
    }
    @GetMapping("send2")
    public String send2(String msg,String group){
        //1.创建消息
//        Message message= MessageBuilder.createMessage(msg,null);
        //2.发送顺序消息 同步
        //syncSendOrderly 参数说明：1.主题 2.消息 Message 对象，字符串 3.消息组名
        template.asyncSendOrderly("xingmq01", msg, group, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("异步发送顺序消息={}",sendResult.getSendStatus().name());
            }
            @Override
            public void onException(Throwable throwable) {

            }
        });
        return "OK";
    }
}
